/*
 * Copyright (c) 2019. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package cn.dyw.ui.component;

import cn.dyw.player.Player;
import cn.dyw.player.PlayerFactory;
import cn.dyw.ui.MainFrame;
import cn.dyw.ui.MainPanel;

import javax.swing.*;

public class MenuOperation extends JMenuBar {

    private JFileChooser jFileChooser;

    private MainPanel mainPanel;

    private JMenu file;

    private Player player;

    public MenuOperation(MainPanel mainPanel) {

        this.mainPanel = mainPanel;

        this.player = PlayerFactory.factory();

        jFileChooser = new JFileChooser();

        file =  new JMenu("文件");
        // 设置助记符为F，按下ALT + F 可以触发该菜单
        file.setMnemonic('F');

        JMenuItem open = new JMenuItem("打开");
        open.addActionListener(e -> {
            int returnVal = jFileChooser.showOpenDialog(this.mainPanel.getMusicPanel());
            // 保存文件从这里入手，输出的是文件名
            if(returnVal == JFileChooser.APPROVE_OPTION) {
                try {
                    player.readyPlay(jFileChooser.getSelectedFile());
                    this.mainPanel.switchStopButtonIcon(0);
                    this.mainPanel.resetProgress();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    System.out.println("播放选择的文件失败");
                }
            }
        });

        file.add(open);
        this.add(file);
    }
}
